const Web3 = require('web3')

if (typeof web3 !== 'undefined') {
    var web3 = new Web3(web3.currentProvider);
} else {
    // set the provider you want from Web3.providers
    var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
}


var chapter_ctr = require('@/contracts/Chapter.json')
var head_ctr = require('@/contracts/Head.json')
var storyIndex_ctr = require('@/contracts/StoryIndex.json')

var CTR_chapter = web3.eth.contract(chapter_ctr.abi)
var CTR_Head = web3.eth.contract(head_ctr.abi)
var abr_StroyIndex = web3.eth.contract(storyIndex_ctr.abi)
var CTR_StroyIndex = abr_StroyIndex.at('0x0831ccc68cD7f39327A126441CA4c973f82e7745')

export function deployChapter(){
    return Promise((resolve,reject)=>{
        CTR_chapter.deploy({
            data:chapter_ctr.bytecode,
            arguments: ["miracle","0xdc20034c256d04a3e16d58d450d48b159e043e39", "0xdc20034c256d04a3e16d58d450d48b159e043e39", "0xdc20034c256d04a3e16d58d450d48b159e043e39", "I'm text", 0]
        }).send({
            from: '0x0ddbCcE72922DAaeb17E3BbB15e8fFF31C20E35b',
            gas: 1500000,
            gasPrice: '30000000000000'
        },(error, transactionHash) => { 
            if(error){
                console.log("error")
                reject(error)
            }else{
                console.log("deploy success", transactionHash)
                resolve(transactionHash)
            }
        })
    })
}
export function addToHead({title,address,prev}){
    CRT_head.methods.addNode(title,address,prev).call().then((flag) => {
        console.log(flag)
    });
}
export function addChapter(){
    var title = "获得秘籍"
    var auther = "0xdc20034c256d04a3e16d58d450d48b159e043e39"
    var owner = "0xdc20034c256d04a3e16d58d450d48b159e043e39"
    var prev = "0xdc20034c256d04a3e16d58d450d48b159e043e39"
    var text = "I'm text"
    var star = 0

    deployChapter(title,auther,owner,prev,text,star).then((chapterAddress)=>{
        addToHead({title,address:chapterAddress,prev})
    })

}

export async function storyList(){
    return new Promise((resolve,reject)=>{
        var list = CTR_StroyIndex.methods.list.getData()
        console.log("list", list)
        // CTR_StroyIndex.methods.sayhello().call().then((list) => {
        //     resolve(list)
        // })
    })
}
export function addStoryToIndex({title,address}){
    return new Promise((resolve,reject)=>{
        CTR_StroyIndex.methods.append.sendTransaction(title,address, {
            from: '0x0ddbCcE72922DAaeb17E3BbB15e8fFF31C20E35b',
            gas: 1500000,
            gasPrice: '30000000000000'
        },(data)=>{
            console.log(data)
        })
    })
}
export function deployStoryHead({title, brief}){
    return new Promise((resolve,reject)=>{
        CTR_Head.deploy({
            data: head_ctr.bytecode,
            arguments: [title, brief]
        }).send({
            from: '0x0ddbCcE72922DAaeb17E3BbB15e8fFF31C20E35b',
            gas: 1500000,
            gasPrice: '3000'
        }).on('receipt', (receipt) => {
            console.log("contractAddress", receipt.contractAddress) // contains the new contract address
            resolve(receipt.contractAddress)
        })
    })
}
